<?php

namespace app\common\model\mycurrency;

use addons\mycurrency\library\Common;
use app\common\model\User;
use Kkokk\Poster\PosterManager;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class OperateDistributionUser extends Model
{

    //use SoftDelete;



    // 表名
    protected $name = 'mycurrency_operate_distribution_user';

    const STATUS_ZHENGCHANG = 1;//状态 - 正常
    const STATUS_JINYONG = 2;//状态 - 禁用

    /**
     * 添加分销员
     * @param int $user_id 用户id
     * @param int $pid 上级id
     */
    public static function register($user_id,$pid = null){
        $user = User::get($user_id);
        $ppid = null;
        if($pid != null){
            $p_distribution_user = self::get($pid);
            if (!$p_distribution_user){
                $pid = null;
            }
            $ppid = $p_distribution_user->pid;
        }
        $default_avatar = OperateDistributionConfig::where(['id' => 1])->value('default_avatar_images');
        $increase = [
            'user_id' => $user_id,
            'full_name' => $user->nickname,
            'mobile' => null,
            'status' => self::STATUS_ZHENGCHANG,
            'money' => 0,
            'pid' => $pid,
            'ppid' => $ppid,
            'createtime' => time(),
            'avatar' => $default_avatar,
        ];
        self::create($increase);
    }

    /**
     * 生成海报
     * @param int $distribution_user_id 分销员id
     */
    public static function poster($distribution_user_id){
        $config = OperateDistributionConfig::where(['id' => 1])->find();//配置信息
        $background_image = Common::GetHost() . $config['background_image'];//海报背景图
        $distribution_user = self::get($distribution_user_id);//用户信息
        if ($distribution_user['avatar']){
            $avatar = $distribution_user['avatar'];
        }else{
            $default_avatar = $config['default_avatar_images'];
            $avatar = Common::GetHost() . $default_avatar;
        }
        $storage_address = 'poster/user/poster' . $distribution_user['id'] . '.png';//time() . rand(10000, 9999999) . '.png';
        $PosterManager = new PosterManager($storage_address); //生成海报，这里写保存路径和文件名，可以指定图片后缀。默认png
        $result = $PosterManager
            ->buildImDst($background_image,750,1458,[255,255,255,127],false)
            ->buildImage($avatar,30,30,0,0,100,100,false,'circle')
            ->buildText($distribution_user['full_name'],['center',-100],60,50,[51, 51, 51,1])
            ->buildQr(Common::GetHost() . "/pid/{$distribution_user['id']}",80,1210,0,0,0,0,6,1)
            ->buildText('扫描进入小程序',['center',85],1230,30,[51, 51, 51,1])
            ->buildText('扫码优惠使用共享台球杆',['center',130],1300,30,[51, 51, 51,1])
            ->buildText('长按二维码即可进入小程序',['center',130],1335,20,[51, 51, 51,1])
            ->getPoster();
        return '/' . $storage_address;
    }

    public function getAvatarAttr($value){
        if($value != null){
            $baseUrl = Common::GetHost();
            return $baseUrl . $value;
        }
        return '';
    }

    public function getPosterImageAttr($value){
        $baseUrl = Common::GetHost();
        return $baseUrl . $value;
    }

//    public function getWechatCodeImageAttr($value){
//        if($value != null){
//            $baseUrl = Common::GetHost();
//            return $baseUrl . $value;
//        }
//        return '';
//    }
//
//    public function getAlipayCodeImageAttr($value){
//        if($value != null){
//            $baseUrl = Common::GetHost();
//            return $baseUrl . $value;
//        }
//        return '';
//    }
}
